std::regex 您所在的位置:网站首页 std regex wchar std::regex

std::regex

2023-02-12 05:30| 来源: 网络整理| 查看: 265

  C++ 语言 标准库头文件 自立与有宿主实现 具名要求 语言支持库 概念库 (C++20) 诊断库 工具库 字符串库 容器库 迭代器库 范围库 (C++20) 算法库 数值库 本地化库 输入/输出库 文件系统库 (C++17) 正则表达式库 (C++11) 原子操作库 (C++11) 线程支持库 (C++11) 技术规范  正则表达式库 类 basic_regex(C++11) sub_match(C++11) match_results(C++11) 算法 regex_match(C++11) regex_search(C++11) regex_replace(C++11) 迭代器 regex_iterator(C++11) regex_token_iterator(C++11) 异常 regex_error(C++11) 特性 regex_traits(C++11) 常量 syntax_option_type(C++11) match_flag_type(C++11) error_type(C++11) 正则表达式文法 Modified ECMAScript-262(C++11)  std::regex_token_iterator 成员函数 regex_token_iterator::regex_token_iterator regex_token_iterator::operator= 比较 regex_token_iterator::operator==regex_token_iterator::operator!=(C++20 前) 观察器 regex_token_iterator::operator*regex_token_iterator::operator-> 修改器 regex_token_iterator::operator++regex_token_iterator::operator++(int)   定义于头文件 template class regex_token_iterator (C++11 起)

std::regex_token_iterator 是访问底层字符序列内每个正则表达式匹配的单独子匹配的只读遗留向前迭代器 (LegacyForwardIterator) 。它亦可用于访问不为给定的正则表达式所匹配的序列部分(例如作为记号化器)。

构造时,它构造一个 std::regex_iterator ,而在每次自增时,它走过请求的来自当前 match_results 的子匹配,并在自增离开上个子匹配时自增底层的 regex_iterator 。

默认构造的 std::regex_token_iterator 是序列尾迭代器。在抵达最后匹配的最后子匹配自增合法的 std::regex_token_iterator 时,它变得等于序列尾迭代器。进一步解引用或自增它引发未定义行为。

在恰好变为序列尾迭代器前,若请求的子匹配下标列表中出现 -1 (非匹配碎片),则 std::regex_token_iterator 可成为后缀迭代器。若解引用这种迭代器,则返回对应最后匹配和序列结尾之间的字符序列的 match_results 。

std::regex_token_iterator 的典型实现保有底层的 std::regex_iterator 、请求的子匹配下标的容器(例如 std::vector )、等于子匹配下标的内部计数器、指向当前匹配的当前子匹配的指向 std::sub_match 指针和含有最近非匹配字符序列的 std::match_results 对象(用于记号化器模式)。

类型要求 -BidirIt 必须满足遗留双向迭代器 (LegacyBidirectionalIterator) 的要求。 特化

定义对常用字符序列类型的数个特化:

定义于头文件   类型 定义 cregex_token_iterator regex_token_iterator wcregex_token_iterator regex_token_iterator sregex_token_iterator regex_token_iterator wsregex_token_iterator regex_token_iterator 成员类型   成员类型 定义 value_type std::sub_match difference_type std::ptrdiff_t pointer const value_type* reference const value_type& iterator_category std::forward_iterator_tag regex_type basic_regex 成员函数 (构造函数) 构造新的 regex_token_iterator (公开成员函数) (析构函数)(隐式声明) 析构 regex_token_iterator ,包含缓存值 (公开成员函数) operator= 赋值内容 (公开成员函数) operator==operator!=(C++20 中移除) 比较两个 regex_token_iterator (公开成员函数) operator*operator-> 访问当前子匹配 (公开成员函数) operator++operator++(int) 推进迭代器到下一个子匹配 (公开成员函数) 注意

程序员负责确保传递给迭代器构造函数的 std::basic_regex 对象活得久于迭代器。因为迭代器存储 std::regex_iterator ,它存储指向 regex 的指针,故在销毁 regex 后自增迭代器导致未定义行为。

示例 运行此代码 #include #include #include #include #include   int main() { std::string text = "Quick brown fox."; // 记号化(非匹配碎片) // 注意仅匹配二次 regex :在获得第三值时迭代器为后缀迭代器。 std::regex ws_re("\\s+"); // 空白符 std::copy( std::sregex_token_iterator(text.begin(), text.end(), ws_re, -1), std::sregex_token_iterator(), std::ostream_iterator(std::cout, "\n"));   // 迭代首个子匹配 std::string html = "

google " "< a HREF =\"http://apiref.com\">cppreference\n

"; std::regex url_re("]*href\\s*=\\s*\"([^\"]*)\"", std::regex::icase); std::copy( std::sregex_token_iterator(html.begin(), html.end(), url_re, 1), std::sregex_token_iterator(), std::ostream_iterator(std::cout, "\n")); }

输出:

Quick brown fox. http://google.com http://apiref.com


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有